﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
using webapp4U.BOL;

public partial class Controls_ControlContact : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<webapp4U.BOL.ContactSetting> obj = webapp4U.BOL.ContactSetting.GetAllContactSetting(1,1000);
            rptContactSetting.DataSource = obj;
            rptContactSetting.DataBind();
        }
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        webapp4U.BOL.Contact obj = new webapp4U.BOL.Contact();
        obj.ContactID = 0;
        obj.CateID = 1; // 1 la Cate cua lien he
        obj.FullName = txtFullName.Text;
        obj.Address = txtAddress.Text;
        obj.Tel = txtPhone.Text;
        obj.Email = txtEmail.Text;
        obj.Title = txtTitle.Text;
        obj.Content = txtContent.Text;
        obj.AddedDate = DateTime.Now;
        webapp4U.BOL.Contact.Insert(obj);

        //PanelMsg.Visible = true;
        //lblMsg.Text = "Thank you! Your message has been sent successfully!";
        //lblMsg.Text = ResourceManager.GetString("Contact_SendSuccessfully");
        lblThongBao.Text = webapp4U.ResourceManager.GetString("Contact_SendSuccessfully");
        UpdatePanelDialog.Update();
        ShowDialog("MessgeBox");

        ContactSetting contact = ContactSetting.GetContactSettingByID(1);
        string EmailTo = contact.Email;

        // goi email cho admin website
        webapp4U.SendMail sendmail = new webapp4U.SendMail();
        sendmail.MailServer = ConfigurationManager.AppSettings["SmtpServer"].ToString();
        sendmail.MailFrom = ConfigurationManager.AppSettings["EmailFrom"].ToString();
        sendmail.MailTo = EmailTo;
        sendmail.Subject = "Thu lien he duoc goi tu website: " +
            ConfigurationManager.AppSettings["Domain"].ToString();

        // noi dung can goi
        StringBuilder strBody = new StringBuilder(string.Empty);
        strBody.AppendLine("<br>");
        strBody.Append("<br> Full Name: " + obj.FullName);
        strBody.AppendLine("<br>");
        strBody.Append("<br> Email: " + obj.Email);
        strBody.AppendLine("<br>");
        strBody.Append("<br> Phone: " + obj.Tel);
        strBody.AppendLine("<br>");
        strBody.AppendLine("<br>");
        strBody.Append("<br> Title: " + obj.Title);
        strBody.AppendLine("<br>");
        strBody.Append(obj.Title);
        sendmail.Body = strBody.ToString();
        try
        {
            webapp4U.SendMail.Send(sendmail);
        }
        catch
        {

        }
        finally
        {
            Form_Clear();
        }

        // xoa sach form
        this.Form_Clear();
    }
    protected void Form_Clear()
    {
        txtFullName.Text = "";
        txtAddress.Text = "";
        txtPhone.Text = "";
        txtEmail.Text = "";
        txtTitle.Text = "";
        txtContent.Text = "";
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        Form_Clear();
    }

    private void CloseDialog(string dialogId)
    {
        string script = string.Format(@"closeDialog('{0}')", dialogId);
        ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), Page.UniqueID, script, true);
    }
    private void ShowDialog(string dialogId)
    {
        string script = string.Format(@"showDialog('{0}')", dialogId);
        ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), Page.UniqueID, script, true);
    }
}
